﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Microsoft.TeamFoundation.VersionControl.Client;
using Microsoft.TeamFoundation.Client;
using System.Text.RegularExpressions;

namespace CommunicationConverter
{
    class Program
    {
        private static TeamFoundationServer tfs = null;
        private static VersionControlServer versionControlServer = null;
        private static Workspace workspace = null;

        static void Main(string[] args)
        {
            List<string> branchs = new List<string>() { @"NET\DEV\CDSB\VN", @"NET\MAIN\VC\VC", @"NET\ACE\P1I7\VC", @"NET\PROD\P1I7\HTX", @"NET\PROD\P1I7\SPK" };

            List<string> filters = new List<string>() { "*.sln", "*.config", "*.csproj", "*.cs" };
            string root = @"C:\DSEPROJ\";
            List<string> folders = new List<string>() { /*"iMercado", "SinaCCO", "SinaCLI", "SinaCOMP", "SinaCOMU", "SinaIAPO",*/ "SinaOMF"/*, "SinaORD", "SinaSOL", "SinaTES"*/ };


            int count = 0, countTotal = 0;

            tfs = TeamFoundationServerFactory.GetServer("utilcorp05");
            versionControlServer = (VersionControlServer)tfs.GetService(typeof(VersionControlServer));
            workspace = versionControlServer.GetWorkspace(root);

            foreach (string folder in Directory.GetDirectories(root))
            {
                if (folders.Contains(Path.GetFileName(folder)))
                {
                    foreach (string branch in branchs)
                    {
                        string path = folder + @"\" + branch;

                        if (Directory.Exists(path))
                        {
                            Console.WriteLine("Alterando pasta: " + path);

                            string[] files = new string[0];
                            foreach (string filter in filters)
                                files = Directory.GetFiles(path, filter, SearchOption.AllDirectories).Concat(files).ToArray();

                            foreach (string file in files)
                            {
                                string originalContent = File.ReadAllText(file, Encoding.Default);

                                string content = originalContent.Replace(@"Culture=Neutral", @"Culture=neutral");

                                // PSE.Communication
                                content = content.Replace(@"Compilados\Communication\PSE.Communication.Adapters.dll", @"Compilados\Framework\Communication\PSE.Framework.Communication.Adapters.dll");
                                content = content.Replace(@"Compilados\Communication\PSE.Communication.Client.dll", @"Compilados\Framework\Communication\PSE.Framework.Communication.Client.dll");
                                content = content.Replace(@"Compilados\Communication\PSE.Communication.Common.dll", @"Compilados\Framework\Communication\PSE.Framework.Communication.Common.dll");
                                content = content.Replace(@"Compilados\Communication\PSE.Communication.Configuration.dll", @"Compilados\Framework\Communication\PSE.Framework.Communication.Configuration.dll");
                                content = content.Replace(@"Compilados\Communication\PSE.Communication.Core.dll", @"Compilados\Framework\Communication\PSE.Framework.Communication.Core.dll");

                                content = content.Replace(@"Compilados\Communication\Spb\PSE.Communication.Spb.Client.dll", @"Compilados\Framework\Communication\Spb\PSE.Framework.Communication.Spb.Client.dll");
                                content = content.Replace(@"Compilados\Communication\Spb\PSE.Communication.Spb.Common.dll", @"Compilados\Framework\Communication\Spb\PSE.Framework.Communication.Spb.Common.dll");


                                content = content.Replace("PSE.Communication.Adapters, Version=1.0.0.0, Culture=neutral, PublicKeyToken=761a7d70d64e39b3", "PSE.Framework.Communication.Adapters, Version=3.0.0.0, Culture=neutral, PublicKeyToken=d12c15b5d58bc696");
                                content = content.Replace("PSE.Communication.Client, Version=1.0.0.0, Culture=neutral, PublicKeyToken=761a7d70d64e39b3", "PSE.Framework.Communication.Client, Version=3.0.0.0, Culture=neutral, PublicKeyToken=d12c15b5d58bc696");
                                content = content.Replace("PSE.Communication.Common, Version=1.0.0.0, Culture=neutral, PublicKeyToken=761a7d70d64e39b3", "PSE.Framework.Communication.Common, Version=3.0.0.0, Culture=neutral, PublicKeyToken=d12c15b5d58bc696");
                                content = content.Replace("PSE.Communication.Configuration, Version=1.0.0.0, Culture=neutral, PublicKeyToken=761a7d70d64e39b3", "PSE.Framework.Communication.Configuration, Version=3.0.0.0, Culture=neutral, PublicKeyToken=d12c15b5d58bc696");
                                content = content.Replace("PSE.Communication.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=761a7d70d64e39b3", "PSE.Framework.Communication.Core, Version=3.0.0.0, Culture=neutral, PublicKeyToken=d12c15b5d58bc696");

                                content = content.Replace("PSE.Communication.Spb.Client, Version=1.0.0.0, Culture=neutral, PublicKeyToken=761a7d70d64e39b3", "PSE.Framework.Communication.Spb.Client, Version=3.0.0.0, Culture=neutral, PublicKeyToken=d12c15b5d58bc696");
                                content = content.Replace("PSE.Communication.Spb.Common, Version=1.0.0.0, Culture=neutral, PublicKeyToken=761a7d70d64e39b3", "PSE.Framework.Communication.Spb.Common, Version=3.0.0.0, Culture=neutral, PublicKeyToken=d12c15b5d58bc696");

                                // using
                                content = content.Replace("PSE.Communication;", "PSE.Framework.Communication;");

                                // Classes
                                content = content.Replace("PSE.Communication.CommunicationComponent", "PSE.Framework.Communication.CommunicationComponent");
                                content = content.Replace("PSE.Communication.CommunicationProcessException", "PSE.Framework.Communication.CommunicationProcessException");
                                content = content.Replace("PSE.Communication.CommunicationStatus", "PSE.Framework.Communication.CommunicationStatus");
                                content = content.Replace("PSE.Communication.ICommunicationComponent", "PSE.Framework.Communication.ICommunicationComponent");
                                content = content.Replace("PSE.Communication.StreamStatus", "PSE.Framework.Communication.StreamStatus");
                                content = content.Replace("PSE.Communication.CommunicationManager", "PSE.Framework.Communication.CommunicationManager");

                                // Namespaces
                                content = content.Replace("PSE.Communication.Messages", "PSE.Framework.Communication.Messages");

                                // Namespaces e nomes de dll
                                content = content.Replace("PSE.Communication.Adapters", "PSE.Framework.Communication.Adapters");
                                content = content.Replace("PSE.Communication.Client", "PSE.Framework.Communication.Client");
                                content = content.Replace("PSE.Communication.Common", "PSE.Framework.Communication.Common");
                                content = content.Replace("PSE.Communication.Configuration", "PSE.Framework.Communication.Configuration");
                                content = content.Replace("PSE.Communication.Core", "PSE.Framework.Communication.Core");

                                content = content.Replace("PSE.Communication.Spb", "PSE.Framework.Communication.Spb");


                                // FileHelpers
                                content = Regex.Replace(content, @"\>(\.\.\\)*((Sinacor\\Mais\\Projetos\\Compilados\\Core\\Communication\\)|(Sinacor\\Mais\\Projetos\\Compilados\\ExternalRef\\))?FileHelpers\.dll", @">\DSEPROJ\PSE\Compilados\Framework\ExternalRef\FileHelpers.dll");
                                content = Regex.Replace(content, @"\>(\.\.\\)*((Sinacor\\Mais\\Projetos\\Compilados\\Core\\Communication\\)|(Sinacor\\Mais\\Projetos\\Compilados\\ExternalRef\\))?FileHelpers\.Common\.dll", @">\DSEPROJ\PSE\Compilados\Framework\ExternalRef\FileHelpers.Common.dll");
                                content = Regex.Replace(content, @"\>(\.\.\\)*((Sinacor\\Mais\\Projetos\\Compilados\\Core\\Communication\\)|(Sinacor\\Mais\\Projetos\\Compilados\\ExternalRef\\))?FileHelpers\.Common\.Converters\.dll", @">\DSEPROJ\PSE\Compilados\Framework\ExternalRef\FileHelpers.Common.Converters.dll");
                                content = Regex.Replace(content, @"\>(\.\.\\)*((Sinacor\\Mais\\Projetos\\Compilados\\Core\\Communication\\)|(Sinacor\\Mais\\Projetos\\Compilados\\ExternalRef\\))?FileHelpers\.ExcelStorage\.dll", @">\DSEPROJ\PSE\Compilados\Framework\ExternalRef\FileHelpers.ExcelStorage.dll");

                                content = content.Replace("FileHelpers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=9b0dac35dd685e79", "FileHelpers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=d12c15b5d58bc696");
                                content = content.Replace("FileHelpers.Common, Version=1.0.0.0, Culture=neutral, PublicKeyToken=9b0dac35dd685e79", "FileHelpers.Common, Version=1.0.0.0, Culture=neutral, PublicKeyToken=d12c15b5d58bc696");
                                content = content.Replace("FileHelpers.Common.Converters, Version=1.0.0.0, Culture=neutral, PublicKeyToken=9b0dac35dd685e79", "FileHelpers.Common.Converters, Version=1.0.0.0, Culture=neutral, PublicKeyToken=d12c15b5d58bc696");
                                content = content.Replace("FileHelpers.ExcelStorage, Version=1.0.0.0, Culture=neutral, PublicKeyToken=9b0dac35dd685e79", "FileHelpers.ExcelStorage, Version=1.0.0.0, Culture=neutral, PublicKeyToken=d12c15b5d58bc696");


                                // Bvmf
                                content = Regex.Replace(content, @"(\.\.\\)*(Sinacor\\Mais\\Projetos\\Compilados\\Core\\Communication\\)?Bvmf\.Communication\.Adapters\.dll", @"\DSEPROJ\PSE\Compilados\Framework\Communication\PSE.Framework.Communication.Adapters.dll");
                                content = Regex.Replace(content, @"(\.\.\\)*(Sinacor\\Mais\\Projetos\\Compilados\\Core\\Communication\\)?Bvmf\.Communication\.Client\.dll", @"\DSEPROJ\PSE\Compilados\Framework\Communication\PSE.Framework.Communication.Client.dll");
                                content = Regex.Replace(content, @"(\.\.\\)*(Sinacor\\Mais\\Projetos\\Compilados\\Core\\Communication\\)?Bvmf\.Communication\.Common\.dll", @"\DSEPROJ\PSE\Compilados\Framework\Communication\PSE.Framework.Communication.Common.dll");
                                content = Regex.Replace(content, @"(\.\.\\)*(Sinacor\\Mais\\Projetos\\Compilados\\Core\\Communication\\)?Bvmf\.Communication\.Configuration\.dll", @"\DSEPROJ\PSE\Compilados\Framework\Communication\PSE.Framework.Communication.Configuration.dll");
                                content = Regex.Replace(content, @"(\.\.\\)*(Sinacor\\Mais\\Projetos\\Compilados\\Core\\Communication\\)?Bvmf\.Communication\.Core\.dll", @"\DSEPROJ\PSE\Compilados\Framework\Communication\PSE.Framework.Communication.Core.dll");

                                content = content.Replace("Bvmf.Communication.Adapters, Version=1.0.0.0, Culture=neutral, PublicKeyToken=9b0dac35dd685e79", "PSE.Framework.Communication.Adapters, Version=3.0.0.0, Culture=neutral, PublicKeyToken=d12c15b5d58bc696");
                                content = content.Replace("Bvmf.Communication.Client, Version=1.0.0.0, Culture=neutral, PublicKeyToken=9b0dac35dd685e79", "PSE.Framework.Communication.Client, Version=3.0.0.0, Culture=neutral, PublicKeyToken=d12c15b5d58bc696");
                                content = content.Replace("Bvmf.Communication.Common, Version=1.0.0.0, Culture=neutral, PublicKeyToken=9b0dac35dd685e79", "PSE.Framework.Communication.Common, Version=3.0.0.0, Culture=neutral, PublicKeyToken=d12c15b5d58bc696");
                                content = content.Replace("Bvmf.Communication.Configuration, Version=1.0.0.0, Culture=neutral, PublicKeyToken=9b0dac35dd685e79", "PSE.Framework.Communication.Configuration, Version=3.0.0.0, Culture=neutral, PublicKeyToken=d12c15b5d58bc696");
                                content = content.Replace("Bvmf.Communication.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=9b0dac35dd685e79", "PSE.Framework.Communication.Core, Version=3.0.0.0, Culture=neutral, PublicKeyToken=d12c15b5d58bc696");

                                // using
                                content = content.Replace("Bvmf.Communication;", "PSE.Framework.Communication;");

                                // Classes
                                content = content.Replace("Bvmf.Communication.CommunicationComponent", "PSE.Framework.Communication.CommunicationComponent");
                                content = content.Replace("Bvmf.Communication.CommunicationProcessException", "PSE.Framework.Communication.CommunicationProcessException");
                                content = content.Replace("Bvmf.Communication.CommunicationStatus", "PSE.Framework.Communication.CommunicationStatus");
                                content = content.Replace("Bvmf.Communication.ICommunicationComponent", "PSE.Framework.Communication.ICommunicationComponent");
                                content = content.Replace("Bvmf.Communication.StreamStatus", "PSE.Framework.Communication.StreamStatus");
                                content = content.Replace("Bvmf.Communication.CommunicationManager", "PSE.Framework.Communication.CommunicationManager");

                                // Namespaces
                                content = content.Replace("Bvmf.Communication.Messages", "PSE.Framework.Communication.Messages");

                                // Namespaces e nomes de dll
                                content = content.Replace("Bvmf.Communication.Adapters", "PSE.Framework.Communication.Adapters");
                                content = content.Replace("Bvmf.Communication.Client", "PSE.Framework.Communication.Client");
                                content = content.Replace("Bvmf.Communication.Common", "PSE.Framework.Communication.Common");
                                content = content.Replace("Bvmf.Communication.Configuration", "PSE.Framework.Communication.Configuration");
                                content = content.Replace("Bvmf.Communication.Core", "PSE.Framework.Communication.Core");


                                if (originalContent != content)
                                {
                                    Console.WriteLine(file);
                                    count++;

                                    // checkout e salvar
                                    workspace.PendEdit(file);
                                    File.WriteAllText(file, content, Encoding.UTF8);
                                }

                            }

                            countTotal += count;

                            Console.WriteLine("Arquivos alterados: " + count);
                            Console.WriteLine();

                            count = 0;
                        }
                    }
                }
            }

            Console.WriteLine("Fim do processamento, arquivos alterados: " + countTotal);
            Console.ReadKey();
        }
    }
}
